package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.health.dao.RoleDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.pojo.Role;
import com.itheima.health.pojo.User;
import com.itheima.health.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 服务交口实现类
 */
@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;

    /**
     * 查询所有角色
     * @return
     */
    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    /**
     * 分页查询所有角色
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return
     */
    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        // 初始化pageHelper(threadLocal存page,size)
        PageHelper.startPage(currentPage, pageSize);
        // 拦截dao查询的结果,故该查询语句一定要在startPage的下一行;
        List<Role> roles = roleDao.findPage(queryString);
        // 处理拦截的结果
        PageInfo<Role> pageInfo = new PageInfo<>(roles);
        List<Role> list = pageInfo.getList();
        long total = pageInfo.getTotal();
        return new PageResult(total, list);
    }
}
